import random
import os


def read_from_file(filepath):
    result_set = set()
    with open(filepath, 'r') as f:
        text = f.read()
        for c in text:
            if 0x4e00 <= ord(c) <= 0x9fff:
                result_set.add(c)
    return result_set

def generate_chinese_characters():
    # chars = read_from_file("source/3500常用字.txt")
    chars = read_from_file("source/7000常用字.txt")
    return chars

def save_to_txt(characters, filename):
    with open(filename, 'w', encoding='utf-8') as file:
        for char in characters:
            file.write(char + '\n')

# 生成10000个汉字
characters = generate_chinese_characters()
# 保存到txt文件
# filename = 'random_chinese_chars_3500.txt'
filename = 'data/random_chinese_chars_7000.txt'
save_to_txt(characters, filename)
print(f'已生成并保存在文件 {os.path.abspath(filename)} 中。')
